<?php
/**
 * @file			pay.mod.php
 */

class pay_mod {

	function pay_mod() {
	}

	/**
	 * 首页
	 */
	function paypal()
	{
		include_once (P_CLASS . '/pay/Paypal.php');

		$gid = V('g:g', 0);
		if(!$gid) die('error gid.');

		$ginfo = DR('grouponCom.getGrouponById', '', $gid);
		$g = $ginfo['rst']['name'];
		$m = $ginfo['rst']['price'] + $ginfo['rst']['freight'] + $ginfo['rst']['price'] * ($ginfo['rst']['taxrate']/100) ;

		$myPaypal = new Paypal();
		$myPaypal->addField('business', PAYPAL_ACCOUNT);
		$myPaypal->addField('currency_code', 'USD');
		$myPaypal->addField('return', URL('pay.paypal_success')); //'http://YOUR_HOST/payment/paypal_success.php'
		$myPaypal->addField('cancel_return', URL('pay.paypal_failure')); //'http://YOUR_HOST/payment/paypal_failure.php'
		$myPaypal->addField('notify_url', URL('pay.paypal_ipn')); //'http://YOUR_HOST/payment/paypal_ipn.php'
		$myPaypal->addField('item_name', $g);
		$myPaypal->addField('amount', $m);
		$myPaypal->addField('item_number', 1);
		$myPaypal->addField('custom', 'xx');
		$myPaypal->enableTestMode();
		$myPaypal->submitPayment();
	}

	/*
	*ipn
	*/
	function paypal_ipn()
	{
		include_once (P_CLASS . '/pay/Paypal.php');

		$myPaypal = new Paypal();
		$myPaypal->ipnLog = TRUE;
		$myPaypal->enableTestMode();
		if ($myPaypal->validateIpn())
		{
			if ($myPaypal->ipnData['payment_status'] == 'Completed')
			{
				 file_put_contents(P_VAR_LOG . '/paypal.txt', 'SUCCESS');
			}
			else
			{
				 file_put_contents(P_VAR_LOG . './paypal.txt', "FAILURE\n\n" . $myPaypal->ipnData);
			}
		}
	}

	/*
	*failure
	*/
	function paypal_success()
	{
		die('支付成功。<a href="'.URL('index').'">返回</a>');
	}

	/*
	*failure
	*/
	function paypal_failure()
	{
		die('支付失败。<a href="'.URL('index').'">返回</a>');
	}

}
